package com.ljm.dao;

import com.ljm.util.StausCode;
import com.ljm.util.DBUtil;
import java.sql.*;

public class LoginDaoImpl implements LoginDao{
    @Override
    public int validateDateBase(String username, String password) {

        //业务代码
        //1.连接数据库
         Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        try {
            conn=DBUtil.getConnection();
            //对sql语句的预编译
            stat = conn.prepareStatement("select * from J_USER where username = ?");
            stat.setString(1,username);
            //执行SQL语句
            rs = stat.executeQuery();
            String rs_result = String.valueOf(rs.next()); //false --> 转换为字符串"false"
            if(rs_result.equals("false")){
                //1标识用户名不存在
                return StausCode.USERNAME_ERROR;
            }else if(rs_result.equals("true")){
                //取出密码继续比较
                String pwd = rs.getString("password");
                //比较
                if (pwd.equals(password)){
                    //2标识验证通过
                    return StausCode.SUCCESS;
                }else {
                    //用户名正确密码错误
                    return StausCode.PASSWORD_ERROR;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,rs,stat);
        }
        //2.sql查询   // 2.1 select * from user where username =username
                     // 2.2 如果用户名正确，则取出用户密码与上传密码进行比较
        //3 根据查询结果返回对应的标识信息

        //应对返回值，未知错误
        return StausCode.UNKNOW_ERROR;
    }
}
